function varargout = DarisDialog(varargin)
% DARISDIALOG MATLAB code for DarisDialog.fig
%      DARISDIALOG, by itself, creates a new DARISDIALOG or raises the existing
%      singleton*.
%
%      H = DARISDIALOG returns the handle to a new DARISDIALOG or the handle to
%      the existing singleton*.
%
%      DARISDIALOG('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DARISDIALOG.M with the given input arguments.
%
%      DARISDIALOG('Property','Value',...) creates a new DARISDIALOG or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DarisDialog_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DarisDialog_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DarisDialog

% Last Modified by GUIDE v2.5 11-Feb-2013 17:11:38

%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.



% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DarisDialog_OpeningFcn, ...
                   'gui_OutputFcn',  @DarisDialog_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


%   - Michael Eager,  (michael.eager@monash.edu)
%   - (c) 2012,  Monash Biomedical Imaging,  Monash University, Australia



% --- Executes just before DarisDialog is made visible.
function DarisDialog_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to DarisDialog (see VARARGIN)

% Choose default command line output for DarisDialog
handles.output = hObject;
handles.CurrentServer='MON-DARIS';
% UIWAIT makes DarisDialog wait for user response (see UIRESUME)
% uiwait(handles.figure1);
status = system('ls ~/.daris');
if status ~= 0
   MakeDARISuserpreferencefile()
end 
handles.AuthcateUsername=GetDARISusername();
% Update handles structure
guidata(hObject, handles);








    
% --- Outputs from this function are returned to the command line.
function varargout = DarisDialog_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

contents = cellstr(get(hObject,'String')) % returns popupmenu1 contents as cell array
handles.CurrentServer = contents{get(hObject,'Value')}; %returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

NewString = get(hObject,'String'); % returns contents of edit1 as text
disp(['NewString ' NewString])
MakeDARISuserpreferencefile(NewString,handles.CurrentServer);
handles.AuthcateUsername=NewString; %GetDARISusername();


% Update handles structure
guidata(hObject, handles);

set(hObject,'String',handles.AuthcateUsername);
disp(handles.AuthcateUsername);





% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
%uname = getenv('MASSIVE_USERNAME')
[status uname] = system('ls ~/.daris');
if status >= 1
 MakeDARISuserpreferencefile('AuthcateUsername',handles.CurrentServer);
end
handles.AuthcateUsername=GetDARISusername();

% Update handles structure
guidata(hObject, handles);
set(hObject,'String',handles.AuthcateUsername);
disp(handles.AuthcateUsername);


% --- Executes on button press in LOGON button
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.output = hObject;
handles.CurrentServer
disp(['awk -F: ''/' handles.CurrentServer '/ {print $1}'' ~/.daris '])
[status domain] = system(['awk -F: ''/' handles.CurrentServer '/ {print $1}'' ~/.daris'])
contents = cellstr(get(handles.edit1,'String'));
addpath(fullfile(pwd,'plugins'))
password = logindlg('Password','only');   %{get(handles.edit1,'Value')}
%domain = 
disp(['bash ./dlogon ' domain])
[status output] = system (['echo "' password '" | bash ./dlogon ' domain]);
if status == 0
  disp(output)
  if ~isempty(strfind(output,'successfully'))
    set (handles.donebutton, 'Enable', 'on')
    close(handles.figure1)
  end
else
  disp('error in dlogon');
  f = warndlg('Unable to login to DaRIS. Try again or seek help with your DaRIS administrator.', 'DaRIS Login Error', 'modal');
end

% --- Executes on button press in DONE button.
function donebutton_Callback(hObject, eventdata, handles)
% hObject    handle to donebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
display 'Completed DaRIS Dialog.'
close(handles.figure1)

% --- Executes on button press in cancelbutton.
function cancelbutton_Callback(hObject, eventdata, handles)
% hObject    handle to cancelbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
display 'Closing DaRIS Dialog'
close(handles.figure1)


function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function p = getpasswd(action)
%GETPASSWD Dialog to get a password.
% p = GETPASSWD opens a password dialog box. p is the password
% given. If the dialog is closed, the Cancel button is pushed,
% or an empty string is given then p = [].

% Jordan Rosenthal, Apr-18-2000
% jr@ece.gatech.edu

if nargin == 0, action = 'Run'; end
OKAY = 1; CANCELLED = 0;

switch action
   %---------------------------------------------------------------------
case 'Run'
   %---------------------------------------------------------------------
   %--- Setup dialog ---%
   dlgPos = getdlgPos;
   hDlg = dialog('pos',dlgPos,'Name','Get Password');
   uicontrol(hDlg,'style','text','units','pixels','pos',[17 54 50 20],...
      'string','Password:','Horiz','Left');
   hTxt = actxcontrol('Forms.TextBox.1',[72 55 175 20],hDlg);
   uicontrol(hDlg,'Style','PushButton','pos',[275 60 75 30],'string','Okay',...
      'callback','getpasswd Okay');
   uicontrol(hDlg,'Style','PushButton','pos',[275 20 75 30],'string','Cancel', ....
      'callback','getpasswd Cancel');
   set(hTxt,'PasswordChar','*');
   set(hDlg,'UserData',CANCELLED);

   %--- Wait for user action ---%
   uiwait(hDlg);

   %--- Process user action ---%
   % If the windows close button is pushed then hDlg will not
   % be a valid handle so must check for that.
   p = [];
   if ishandle(hDlg)
      OKAYPUSHED = get(hDlg,'UserData');
      if OKAYPUSHED
         p = deblank( get(hTxt,'Text') );
      end
      delete(hDlg);
   end

   %---------------------------------------------------------------------
case 'Okay'
   %---------------------------------------------------------------------
   set(gcbf,'UserData',OKAY);
   uiresume(gcbf);

   %---------------------------------------------------------------------
case 'Cancel'
   %---------------------------------------------------------------------
   set(gcbf,'UserData',CANCELLED);
   uiresume(gcbf);

   %---------------------------------------------------------------------
otherwise
   %---------------------------------------------------------------------
   error('Illegal action.');
end

%---------------------------------------------------------------------
%---------------------------------------------------------------------

function dlgPos = getdlgPos()
%GETDLGPOS This function gets the position setting for the dialog.
OldUnits = get(0,'Units');
set(0,'units','pixels');
ScreenSize = get(0,'ScreenSize');
set(0,'units',OldUnits);
center = ScreenSize(3:4)/2;
dlgPos = [center(1)-188 center(2)-50 375 105];


% --- Executes on key press with focus on pushbutton2 and none of its controls.
function pushbutton2_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
pushbutton2_Callback(hObject, eventdata, handles)


% --- Executes on key press with focus on donebutton and none of its controls.
function donebutton_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to donebutton (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
donebutton_Callback(hObject, eventdata, handles)
